package com.sjz.test2;

import java.time.DayOfWeek;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.Period;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Set;

import org.junit.Test;

public class TestLocalDateTime {

	
	////1. LocalDate、LocalTime、LocalDateTime
	@Test
	public void test1() {
		LocalDateTime localDateTime =LocalDateTime.now();
		System.out.println(localDateTime);//2023-06-17T18:44:02.590
		LocalDateTime ld = LocalDateTime.of(2016, 11, 3, 9, 12,22);   
		System.out.println(ld);//2016-11-03T09:12:22
		LocalDateTime ld3 = ld.plusYears(3);
		System.out.println(ld3);//2019-11-03T09:12:22
		LocalDateTime ld4 = ld3.minusMonths(2);
		System.out.println(ld4);//2019-09-03T09:12:22
		
		System.out.println(localDateTime.getYear());
		System.out.println(localDateTime.getMonthValue());
		System.out.println(localDateTime.getDayOfMonth());
		System.out.println(localDateTime.getHour());
		System.out.println(localDateTime.getMinute());
		System.out.println(localDateTime.getSecond());
	}
	
    //Instant : 时间戳。 （使用 Unix 元年  1970年1月1日 00:00:00 所经历的毫秒值）
	@Test
	public void test2() {
		Instant instant =Instant.now();
		System.out.println(instant); //2023-06-17T11:38:02.473Z
        
		OffsetDateTime atOffset = instant.atOffset(ZoneOffset.ofHours(8));
		System.out.println(atOffset);//2023-06-17T19:38:02.473+08:00

		System.out.println(instant.getNano());//473000000
		
		Instant ins2 = Instant.ofEpochSecond(5);//1970-01-01T00:00:05Z
		System.out.println(ins2);
	}
	
	//Duration : 用于计算两个“时间”间隔
	 //Period : 用于计算两个“日期”间隔
	@Test
	public void test3() {
		Instant ins1 = Instant.now();
		System.out.println("--------------------");
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
		}
		Instant ins2 = Instant.now();
		System.out.println("所耗费时间为：" + Duration.between(ins1, ins2));
		System.out.println("----------------------------------");
		
		LocalDate ld1 = LocalDate.now();
		LocalDate ld2 = LocalDate.of(2011, 1, 1);
		
		Period pe = Period.between(ld2, ld1);
		System.out.println(pe.getYears());
		System.out.println(pe.getMonths());
		System.out.println(pe.getDays());
	}
	 
	
	//4. TemporalAdjuster : 时间校正器
	@Test
	public void test4() {
		LocalDateTime ldt = LocalDateTime.now();
		System.out.println(ldt);  //2023-06-17T19:50:44.371

		LocalDateTime ldt2 = ldt.withDayOfMonth(10);
		System.out.println(ldt2);//2023-06-10T19:50:44.371

		LocalDateTime ldt3 = ldt.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));
		System.out.println(ldt3);//2023-06-18T19:50:44.371

		// 自定义：下一个工作日
		LocalDateTime ldt5 = ldt.with((l) -> {
			LocalDateTime ldt4 = (LocalDateTime) l;

			DayOfWeek dow = ldt4.getDayOfWeek();

			if (dow.equals(DayOfWeek.FRIDAY)) {
				return ldt4.plusDays(3);
			} else if (dow.equals(DayOfWeek.SATURDAY)) {
				return ldt4.plusDays(2);
			} else {
				return ldt4.plusDays(1);
			}
		});

		System.out.println(ldt5);//2023-06-19T19:50:44.371
	}
	
	//5. DateTimeFormatter : 解析和格式化日期或时间
	@Test
	public void test5() {
		DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss E");

		LocalDateTime ldt = LocalDateTime.now();
		String strDate = ldt.format(dtf);

		System.out.println(strDate);//2023年06月17日 19:54:45 星期六

		LocalDateTime newLdt = ldt.parse(strDate, dtf);
		System.out.println(newLdt); //2023-06-17T19:54:45
	}
	
	/***
	 * Java中的ZoneId常用值
	 */
	@Test
	public void test6(){
		Set<String> set = ZoneId.getAvailableZoneIds();
		set.forEach(System.out::println);
	}
	
	//6.ZonedDate、ZonedTime、ZonedDateTime ： 带时区的时间或日期
	@Test
	public void test7() {
		LocalDateTime ldt = LocalDateTime.now(ZoneId.of("Asia/Shanghai"));
		System.out.println(ldt);//2023-06-17T19:58:42.144

		ZonedDateTime zdt = ZonedDateTime.now(ZoneId.of("US/Pacific"));
		System.out.println(zdt);//2023-06-17T04:58:42.148-07:00[US/Pacific]
	}
}
